package advance.JVM.concurrent;

import java.util.concurrent.TimeUnit;

/**
 * description: <br>
 * createTime: 2018/11/2816:27 <br>
 *
 * @author zzh
 */
public class TestUnsafePublish {
    public static class TwoTread{
        private HolderN holderN;
        public void test() throws InterruptedException {
            new Thread(() -> {
                holderN = new HolderN(24);
            }).start();
            while(holderN == null) {
                TimeUnit.NANOSECONDS.sleep(1);
            }
            holderN.assertN();
        }
    }


    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 1000_000; i++) {
            new TwoTread().test();
        }
    }
}
class HolderN{
    private int n;
    public HolderN(int n) {
        this.n = n;
    }
    public void assertN() {
        if(n != n) {
            throw new AssertionError("n != n");
        }
    }
    public HolderN setN(int n) {
        this.n = n;
        return this;
    }
}